<?php

require_once './application/controllers/ControllerBase.php';
require_once './application/core/Common/CommonCore.php';
require_once './application/core/Dashboard/DashboardCore.php';
require_once './application/core/Dashboard/monitorCore.php';
require_once './application/core/Toolbar/ToolbarCore.php';

///
/// Classe gerant les connections / deconnections des utilisateurs
///
class DashboardController extends ControllerBase
{

    function indexAction()
    {
        $this->verifConnexion();
        $session = new zend_session_namespace('session');
        $this->renderTabControl();
        $dashboardCore = new DashboardCore($this);
        $lst = $dashboardCore->getListDashBoardByUser($session->idClient);
        $this->view->refresh_title = constant('DASHBOARD_BODY_REFRESH');
        if ($lst[0] == 0)
        {
            $this->view->content = DASHBOARD_BODY_INDEX_EMPTY;
            $this->view->content .= '<br/>';
            $this->view->content .= DASHBOARD_BODY_INDEX_EMPTY_LINK;
            $this->view->content .= " <a href=\"javascript:ajouterItem('Configuration : Tableau de bord', 'dashboard/configuration/')\">";
            $this->view->content .= DASHBOARD_BODY_INDEX_EMPTY_LINK2;
            $this->view->content .= "</a>";
            $this->view->refresh = 'false';
        }
        else
            $this->view->refresh = 'true';
        $this->view->column1 = $lst[1][0];
        $this->view->column2 = $lst[1][1];
        $this->view->column3 = $lst[1][2];
        $this->view->column4 = $lst[1][3];
    }

    function configurationAction()
    {
        $this->verifConnexion();
        $session = new zend_session_namespace('session');
        $this->renderTabControl();
        $dashboardCore = new DashboardCore($this);
        $lst = $dashboardCore->getListDashBoard($session->idClient);
        $this->view->lst1 = $lst[0];
        $this->view->lst2 = $lst[1];
    }

    function ajoutermoduleutilisateurAction()
    {
        $this->verifConnexion();
        $this->noRender();
        $dashboardCore = new DashboardCore($this);
        $params = $this->_request->getParams();
        if (isset($params["user"]) && isset($params["module"]))
        {
            echo $dashboardCore->ajouterModuleUtilisateur($params["user"], $params["module"]);
        }
    }

    function retirermoduleutilisateurAction()
    {
        $this->verifConnexion();
        $this->noRender();
        $dashboardCore = new DashboardCore($this);
        $params = $this->_request->getParams();
        if (isset($params["user"]) && isset($params["module"]))
        {
            echo $dashboardCore->retirerModuleUtilisateur($params["user"], $params["module"]);
        }
    }

    function ajoutermoduleAction()
    {
        $this->verifConnexion();
        $this->noRender();
        $dashboard = new DashboardCore($this);
        if ($dashboard->installerModule($_FILES, $_REQUEST['folder']))
        {
            echo "1";
        } else
        {
            echo "0";
        }
    }

    function selectionnerformAction()
    {
        $this->verifConnexion();
        $this->renderTabControl();
        $session = new zend_session_namespace('session');
        $dashboardCore = new DashboardCore($this);
        $lst = $dashboardCore->getListDashBoard($session->idClient);
        $this->view->lst1 = $lst[0];
        $this->view->lst2 = $lst[1];
    }

    function ajouterformAction()
    {
        $this->verifConnexion();
        $this->renderTabControl();
    }

    function desinstallermoduleAction()
    {
        $this->verifConnexion();
        $this->noRender();
        $params = $this->_request->getParams();
        $dashboard = new DashboardCore($this);
        if ($dashboard->desinstallerModule($params['nom'], 1))
        {
            echo "1";
        } else
        {
            echo "0";
        }
    }

    function showmoduleAction()
    {
        $this->verifConnexion();
        $params = $this->_request->getParams();
        $page = "";
        if (isset($params["module"]))
        {
            if (file_exists(APPLICATION_PATH . "/application/core/Module/" . $params["module"] . ".php"))
            {
                require_once(APPLICATION_PATH . "/application/core/Module/" . $params["module"] . ".php");
                $obj = new ReflectionClass($params["module"] . "Module");
                if ($obj->hasMethod('execute'))
                {
                    $method = $obj->getMethod("execute");
                    if ($method->isPublic() && $method->isStatic())
                    {
                        $method->invoke($obj);
                        $this->view->params = $method->invoke($obj);
                    } else
                    {
                        $this->view->error = DASHBOARD_BODY_ERROR_EXECUTE_MODULE;
                    }
                } else
                {
                    $this->view->error = DASHBOARD_BODY_ERROR_EXECUTE_MODULE;
                }
            } else
            {
                $this->view->error = DASHBOARD_BODY_ERROR_MODULE_UNKNOW;
            }
        }
        $this->view->page = "/module/" . $params["module"] . ".phtml";
    }

    function ajoutermonitorAction()
    {
        $this->verifConnexion();
        $this->noRender();
        $t = new monitorCore($this);
        $t->ajouterMoniteur();
    }

}

